﻿using System;
using System.Text.RegularExpressions;

namespace CaifenWeb.UtilityCls
{
    public  class DataStateValidator {


        /// <summary>
        /// 检查昵称是否合法
        /// </summary>
        /// <param name="obj"></param>
        /// <returns>返回值：1=正常；-1=空字符串；-2 长度小于2；-3=全数字；-4=非法字符串.</returns>
        public static void NickNameCheck(string nickName,out int code,out string msg)
        {
            code = 1;
            msg = "正常";
            Regex regex = new Regex(@"^[0-9]+$");
            if (string.IsNullOrWhiteSpace(nickName))
            {
                code = -1;
                msg = "昵称不能为空";
            }
            else if (nickName.Length < 2)
            {
                code = -2;
                msg = "昵称至少2位数";
            }
            else if (regex.IsMatch(nickName))
            {
                code = -3;
                msg = "昵称不能全为数字";
            }
            else
            {
                regex = new Regex(@"^[a-zA-Z0-9\u4e00-\u9fa5_-]+$");
                if (!regex.IsMatch(nickName))
                {
                    code = -4;
                    msg = "昵称含有非法字符";
                }
            }
        }

        /// <summary>
        /// 检查帐号是否符合规则。
        /// </summary>
        /// <param name="obj"></param>
        /// <returns>返回值：1=正常；-1=空字符串；-2=小于6个字符；-3=大于18个字符；-4=有非法字符。</returns>
        public static int MemberIDCheck(string memberID)
        {
            int result = 1;
            Regex regex = new Regex("^[a-zA-Z][a-z0-9A-Z_]+$");
            if (string.IsNullOrEmpty(memberID))
            { result = -1; }
            if (memberID.StartsWith("renren") || memberID.StartsWith("tengxun") || memberID.StartsWith("sina"))
            { result = -5; }
            else if (memberID.Length < 6)
            { result = -2; }
            else if (memberID.Length > 18)
            { result = -3; }
            else if (!regex.IsMatch(memberID))
            { result = -4; }
            return result;
        }

        public static int EmailCheck(string email)
        {
            int result = 1;
            Regex regex = new Regex(@"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$");
            if (string.IsNullOrWhiteSpace(email))
            { result = -1; }
            else if (!regex.IsMatch(email))
            { result = -2; }
            return result;
        }
    
    }

}